package com.gitee.urick.union.api.union.dto.base;

import cn.hutool.core.util.StrUtil;
import com.gitee.urick.union.api.constants.Constants;
import com.gitee.urick.union.utils.TipsCode;
import lombok.Getter;
import lombok.Setter;

import java.util.StringJoiner;

/**
 * 修改记录
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 修改日期				修改者						备注信息
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 2020年08月19日			kkroc							初始化版本
 */
public class BaseRes {
    @Setter
    @Getter
    /**
     * 响应状态：SUCCESS-处理成功，PROCESSING-处理中，FAIL-处理失败；
     *
     */
    private String status;
    /**
     * 响应消息
     */
    @Getter
    private String msg;

    public void generateSign() {


    }

    public BaseRes success() {
        return success("成功");
    }

    public BaseRes success(String msg) {
        this.status = Constants.Status.SUCCESS.name();
        this.msg = msg;
        return this;
    }

    public BaseRes error(TipsCode code) {
        this.status = Constants.Status.FAIL.name();
        addMsg(code.getPattern());
        return this;
    }


    public BaseRes error(String msg) {
        this.status = Constants.Status.FAIL.name();
        addMsg(msg);
        return this;
    }

    public BaseRes error() {
        this.status = Constants.Status.FAIL.name();
        addMsg("失败");
        return this;
    }

    private void addMsg(String newMsg) {
        if (StrUtil.isBlank(newMsg)) msg = newMsg;
        StringJoiner joiner = new StringJoiner(StrUtil.COMMA);
        joiner.add(msg).add(newMsg);
        msg = joiner.toString();
    }
}
